home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Interapplication Communication / MenuScripter 3.1 / Sources / MSAEScript.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-20  |  17.4 KB  |  656 lines  |  [TEXT/MPCC]

  1. /*
  2.     MSAEScript.c
  3.     
  4.     Version 3.1
  5.     
  6.     Copyright © 1995 Apple Computer, Inc., all rights reserved.
  7.     
  8.     MenuScripter by Nigel Humphreys and Jon Lansdell
  9.     AppleEvent to script extensions by Greg Sutton
  10. */
  11.  
  12. #include <AppleScript.h>
  13. #include <Resources.h>
  14. #include <OSA.h>
  15. #include <AERegistry.h>
  16. #include <ASRegistry.h>
  17. #include <TextUtils.h>
  18.  
  19. #ifndef THINK_C
  20. #include <PLStringFuncs.h>
  21. #endif
  22.  
  23. #include "MSAEScript.h"
  24. #include "MSAppleEvents.h"
  25.     
  26. extern ComponentInstance gScriptingComponent;
  27.  
  28. #ifdef THINK_C
  29.     extern pascal StringPtr PLstrcpy(StringPtr str1, StringPtr str2);
  30.     extern pascal StringPtr    PLstrcat(StringPtr str1, StringPtr str2);
  31. #endif
  32.  
  33. short            gAppVol;
  34. long            gAppDir;
  35.  
  36. OSAID            gScript1ID;
  37. OSAID            gScript2ID;
  38. // Script 3 targets a script application - it's script is not loaded
  39. OSAID            gScript4ID;
  40. OSAID            gScript5ID;
  41.  
  42. // Variables to target script application
  43. ProcessSerialNumber    gScriptAppPSN;
  44. AEDesc                gScriptAppAddress;
  45.  
  46. Str31            gScript1Name = "\pscript shift";
  47. Str31            gScript2Name = "\pscript datestring";
  48. Str31            gScript3Name = "\pscript topseeturvee";
  49. Str31            gScript4Name = "\pscript changecreator";
  50. Str31            gScript5Name = "\pscript get/set selection";
  51.  
  52.  
  53. //    Use process manager to find the volume and directory of this application.
  54.  
  55. pascal OSErr    GetVolumeAndDirectory(void)
  56. {
  57.     ProcessSerialNumber        PSN;
  58.     ProcessInfoRec            info;
  59.     Str31                    name;
  60.     FSSpec                    specRec;
  61.     OSErr                    err;
  62.     
  63.     gAppVol = 0;
  64.     gAppDir = 0;
  65.  
  66.     err = GetCurrentProcess(&PSN);
  67.  
  68.     if (err == noErr)
  69.     {
  70.         info.processName = name;
  71.         info.processAppSpec = &specRec;
  72.         err = GetProcessInformation(&PSN, &info);
  73.         
  74.         if (err == noErr)
  75.         {
  76.             gAppVol = specRec.vRefNum;
  77.             gAppDir = specRec.parID;
  78.         }
  79.     }
  80.     
  81.     return(err);
  82. }
  83.  
  84.  
  85. // Launch an application into the background given an FSSpec
  86.  
  87. pascal OSErr FSSpecLaunchApplication(const FSSpec *fileSpec, ProcessSerialNumber *PSN)
  88. {
  89.     LaunchParamBlockRec launchRec;
  90.     OSErr                err;
  91.  
  92.     launchRec.launchBlockID = extendedBlock;
  93.     launchRec.launchEPBLength = extendedBlockLen;
  94.     launchRec.launchFileFlags = 0;
  95.     launchRec.launchControlFlags = launchContinue + launchNoFileFlags + launchDontSwitch;
  96.     launchRec.launchAppSpec = (FSSpecPtr)fileSpec;
  97.     launchRec.launchAppParameters = nil;
  98.     
  99.     err = LaunchApplication(&launchRec);
  100.  
  101.     if (err == noErr)
  102.         *PSN = launchRec.launchProcessSN;
  103.         
  104.     return(err);
  105. }
  106.  
  107. // Set up the scripts we can send AppleEvents to them.
  108. // Launch the script application.
  109.  
  110. pascal void SetUpScripts(void)
  111. {
  112.     FSSpec        fileSpec;
  113.     OSErr       err;
  114.  
  115.     fileSpec.vRefNum = gAppVol;
  116.     fileSpec.parID = gAppDir;
  117.  
  118.     PLstrcpy(fileSpec.name,gScript1Name);
  119.     LoadScriptFromFile(&fileSpec, &gScript1ID);
  120.  
  121.     PLstrcpy(fileSpec.name,gScript2Name);
  122.     LoadScriptFromFile(&fileSpec, &gScript2ID);
  123.  
  124.     PLstrcpy(fileSpec.name,gScript3Name);
  125.     err = FSSpecLaunchApplication(&fileSpec,&gScriptAppPSN);
  126.     if (err == noErr) 
  127.         AECreateDesc(typeProcessSerialNumber,(Ptr)&gScriptAppPSN, 
  128.                      sizeof(ProcessSerialNumber), &gScriptAppAddress);
  129.     else
  130.         gScriptAppAddress.dataHandle = NULL;
  131.  
  132.     PLstrcpy(fileSpec.name,gScript4Name);
  133.     LoadScriptFromFile(&fileSpec, &gScript4ID);
  134.  
  135.     PLstrcpy(fileSpec.name,gScript5Name);
  136.     LoadScriptFromFile(&fileSpec, &gScript5ID);
  137.  
  138. }
  139.  
  140. //     Given a FileSpec open the resource fork and Load the first script
  141. //    then release the resource and close the file.
  142. //    The theScriptID will be set to kOSANullScript if any problems - so check against this for use.
  143.  
  144. pascal OSErr    LoadScriptFromFile(FSSpec *fileSpec, OSAID *theScriptID)
  145. {
  146.     OSErr       ignoreErr;
  147.     Handle      myScript;
  148.     AEDesc      myLoadScriptDesc;
  149.     short        fileRef;
  150.     OSErr        err;
  151.  
  152.     *theScriptID = kOSANullScript;
  153.         
  154.     fileRef = FSpOpenResFile(fileSpec, fsRdPerm);
  155.     err = ResError();
  156.  
  157.     if (err == noErr)
  158.     {
  159.         myScript  = Get1Resource(kOSAScriptResourceType, 128);
  160.         if (myScript != NULL)
  161.         {
  162.             HLock(myScript);
  163.         
  164.             err = AECreateDesc(typeOSAGenericStorage,(Ptr)*myScript,
  165.                                 GetHandleSize(myScript),&myLoadScriptDesc);
  166.                 
  167.             HUnlock(myScript);
  168.             
  169.             if (err==noErr)
  170.               {
  171.                 err = OSALoad(gScriptingComponent,&myLoadScriptDesc,
  172.                               kOSAModeNull,theScriptID);
  173.                }
  174.             
  175.             ignoreErr = AEDisposeDesc(&myLoadScriptDesc);
  176.             ReleaseResource(myScript);
  177.         }
  178.         CloseResFile(fileRef);
  179.     }
  180.    
  181.     return(err);
  182. }
  183.  
  184. // Store the scipt ID as a 'scpt' resource, ID 128, in file given.
  185.  
  186. pascal OSErr    StoreScriptToFile(FSSpec *fileSpec, OSAID theScriptID)
  187. {
  188.     short        fileRef;
  189.     OSErr        err;
  190.     
  191.     if (theScriptID == kOSANullScript)
  192.         return(noErr);
  193.  
  194.     fileRef = FSpOpenResFile(fileSpec, fsWrPerm);
  195.     err = ResError();
  196.     
  197.     if (err == noErr)
  198.     {
  199.         AEDesc        scriptData = {typeNull,NULL};
  200.         
  201.         err = (OSErr)OSAStore(gScriptingComponent, theScriptID, typeOSAGenericStorage,
  202.                                                 kOSAModeNull, &scriptData);
  203.         
  204.         if (err == noErr)
  205.         {                            // Write over current script
  206.             Handle    h = Get1Resource(kOSAScriptResourceType, 128);
  207.             if (h)
  208.             {
  209.                 RemoveResource(h);    // Disposed of on UpdateResFile
  210.                 err = ResError();
  211.             }
  212.             
  213.             if (err == noErr)
  214.             {
  215.                 h = scriptData.dataHandle;
  216.                 HLock(h);
  217.                 HandToHand(&h);                                 
  218.                 AddResource(h, typeOSAGenericStorage, 128, "\p");
  219.                 UpdateResFile(fileRef);
  220.                 HUnlock(h);
  221.             }
  222.             
  223.             AEDisposeDesc(&scriptData);
  224.         }
  225.         
  226.         CloseResFile(fileRef);
  227.     }
  228.     
  229.     return(err);
  230. }
  231.  
  232. // Quit the script application, store changes to scripts and dispose of scripts.
  233.  
  234. pascal OSErr CleanUpAEScripts(void)
  235. {
  236.     AppleEvent         myAppleEvent = {typeNull,NULL},
  237.                     ignoreReply = {typeNull,NULL};
  238.     FSSpec            fileSpec;
  239.     OSErr            err;
  240.     
  241.                 // Quit the script application we launched
  242.     if (gScriptAppAddress.dataHandle)
  243.     {
  244.         err = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &gScriptAppAddress,
  245.                                                                         0, 0, &myAppleEvent);
  246.         
  247.         if (err == noErr)
  248.             err = AESend(&myAppleEvent, &ignoreReply, kAENoReply + kAEAlwaysInteract,
  249.                                     kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  250.         
  251.         AEDisposeDesc(&myAppleEvent);
  252.     }
  253.  
  254.         // Store the scripts used - this saves any changed properties and aliases
  255.     fileSpec.vRefNum = gAppVol;
  256.     fileSpec.parID = gAppDir;
  257.  
  258.     PLstrcpy(fileSpec.name,gScript1Name);
  259.     StoreScriptToFile(&fileSpec, gScript1ID);
  260.  
  261.     PLstrcpy(fileSpec.name,gScript2Name);
  262.     StoreScriptToFile(&fileSpec, gScript2ID);
  263.  
  264.     PLstrcpy(fileSpec.name,gScript4Name);
  265.     StoreScriptToFile(&fileSpec, gScript4ID);
  266.  
  267.     PLstrcpy(fileSpec.name,gScript5Name);
  268.     StoreScriptToFile(&fileSpec, gScript5ID);
  269.  
  270.     err = OSADispose(gScriptingComponent, gScript1ID);
  271.     err = OSADispose(gScriptingComponent, gScript2ID);
  272.     err = OSADispose(gScriptingComponent, gScript4ID);
  273.     err = OSADispose(gScriptingComponent, gScript5ID);
  274.     
  275.     return(err);
  276. }
  277.  
  278.  
  279. //    Calls a script subroutine that takes two window names or numbers.
  280. //    If a window doesn't exist then it is created, one window is then
  281. //    moved below the other.
  282. //    Uses predefined label parameters.
  283. //        on shift around winMove below winStay
  284.  
  285. pascal OSErr ExecuteScript1(DPtr theDoc)
  286. {
  287. #pragma unused (theDoc)
  288.  
  289.     AppleEvent         myAppleEvent = {typeNull,NULL},
  290.                     ignoreReply = {typeNull,NULL};
  291.     AEDesc            selfAddress;
  292.     OSErr           myErr;
  293.     Str255          handlerName = "\pshift",
  294.                     below = "\pBelow",
  295.                     above = "\pAbove";
  296.     
  297.     if (gScript1ID == kOSANullScript)
  298.         return(noErr);
  299.     
  300.     myErr = MakeSelfAddress(&selfAddress);
  301.     if (myErr != noErr) return noErr; 
  302.  
  303.             // Set up a subroutine AppleEvent
  304.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  305.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  306.    
  307.            // Add the name of the subroutine
  308.     if (myErr == noErr)
  309.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  310.                                                                    (Ptr)&handlerName[1], handlerName[0]);
  311.            
  312.            // Add the first predefined label parameter                                                
  313.     if (myErr == noErr)
  314.         myErr = AEPutParamPtr(&myAppleEvent,    keyASPrepositionAround, typeChar,    
  315.                                                                 (Ptr)&below[1], below[0]);
  316.                                                                        
  317.            // Add the second predefined label parameter                                                
  318.     if (myErr == noErr)
  319.         myErr = AEPutParamPtr(&myAppleEvent,    keyASPrepositionBelow, typeChar,    
  320.                                                                 (Ptr)&above[1], above[0]);
  321.         
  322.     if (myErr == noErr)
  323.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript1ID,
  324.                                         kOSAModeAlwaysInteract, &ignoreReply);
  325.                                         
  326.       AEDisposeDesc(&selfAddress);
  327.     AEDisposeDesc(&myAppleEvent);
  328.     AEDisposeDesc(&ignoreReply);
  329.     
  330.     return(myErr);
  331. }
  332.     
  333. //    Calls a subroutine that returns the date and/or time depending
  334. //    on parameters sent.
  335. //    Uses subroutine defined label parameters.
  336. //        on datestring given wDate:fDate, wTime:fTime
  337.  
  338. pascal OSErr ExecuteScript2(DPtr theDoc)
  339. {
  340. #pragma unused (theDoc)
  341.  
  342.     AppleEvent         myAppleEvent = {typeNull,NULL},
  343.                     myReply = {typeNull,NULL};        // Need to NULL for OSADoEvent() routine
  344.     AEDesc            selfAddress,
  345.                     textDesc;
  346.     AEDescList        paramList = {typeNull,NULL};
  347.     OSErr           myErr;
  348.     Str255          pStr = "\pdatestring";
  349.     
  350.     if (gScript2ID == kOSANullScript)
  351.         return(noErr);
  352.  
  353.     myErr = MakeSelfAddress(&selfAddress);
  354.     if (myErr != noErr) return noErr;
  355.  
  356.                 // Create an AppleScript subroutine AppleEvent
  357.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  358.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  359.    
  360.                // Specify the routine by it's name
  361.     if (myErr == noErr)
  362.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  363.                                                                    (Ptr)&pStr[1], pStr[0]);
  364.  
  365.             // Create list for user defined label parameters
  366.     if (myErr == noErr)
  367.         myErr = AECreateList(NULL, 0 , false, ¶mList);
  368.  
  369.     if (myErr == noErr)
  370.     {            // Make sure label name is in lower case
  371.         PLstrcpy(pStr,"\pwdate");
  372.         myErr = AEPutPtr(¶mList, 0 ,typeChar ,(Ptr)&pStr[1],  pStr[0]);
  373.         if (myErr == noErr) myErr = AEPutPtr(¶mList, 0, typeTrue , NULL,  0);
  374.     }
  375.  
  376.     if (myErr == noErr)
  377.     {
  378.         PLstrcpy(pStr,"\pwtime");
  379.         myErr = AEPutPtr(¶mList, 0 ,typeChar ,(Ptr)&pStr[1],  pStr[0]);
  380.         if (myErr == noErr)  AEPutPtr(¶mList, 0, typeFalse , NULL,  0);
  381.     }
  382.  
  383.             // Add list to AppleEvent
  384.     if (myErr == noErr)
  385.         myErr = AEPutParamDesc(&myAppleEvent, keyASUserRecordFields, ¶mList);
  386.  
  387.     if (myErr == noErr)
  388.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript2ID,
  389.                                                     kOSAModeAlwaysInteract, &myReply);
  390.       
  391.       AEDisposeDesc(&selfAddress);
  392.     AEDisposeDesc(&myAppleEvent);
  393.     AEDisposeDesc(¶mList);
  394.     
  395.     if (myErr == noErr)
  396.         myErr = GetTextDescFromReply(&myReply, &textDesc);
  397.  
  398.     if (myErr == noErr)
  399.            myErr = SetSelection(&textDesc);
  400.         
  401.     AEDisposeDesc(&textDesc);
  402.     AEDisposeDesc(&myReply);
  403.         
  404.     return(myErr);
  405. }
  406.  
  407. //    First of all gets the current selection. Then calls a script application
  408. //    subroutine to twist the text in the selection. Finally it sets the selection
  409. //    with the result.
  410. //    Uses a positional parameter.
  411. //        on topseeturvee(someText)
  412.  
  413. pascal OSErr ExecuteScript3(DPtr theDoc)
  414. {
  415. #pragma unused (theDoc)
  416.  
  417.     AppleEvent         myAppleEvent = {typeNull,NULL},
  418.                     myReply = {typeNull,NULL};
  419.     AEDescList       paramList = {typeNull,NULL},
  420.                     textDesc;
  421.     OSErr           myErr;
  422.     Str255          pStr = "\ptopseeturvee";
  423.     
  424.     if (! gScriptAppAddress.dataHandle)
  425.         return(noErr);
  426.         
  427.         // Get the current selection of the front window
  428.     myErr = GetSelection(&textDesc);
  429.     
  430.         // Create a subroutine AppleEvent
  431.     if (myErr == noErr)
  432.         myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &gScriptAppAddress,
  433.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  434.    
  435.            // Add the name of the subroutine
  436.     if (myErr == noErr)
  437.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  438.                                                                    (Ptr)&pStr[1], pStr[0]);
  439.  
  440.         // Create positional parameter list
  441.     if (myErr == noErr)
  442.         myErr = AECreateList(NULL, 0 ,false, ¶mList);
  443.         
  444.         // Add textDesc as positional parameter
  445.     if (myErr == noErr)
  446.         myErr = AEPutDesc(¶mList, 0, &textDesc);
  447.  
  448.         // Add positional parameter list
  449.     if (myErr == noErr)
  450.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, ¶mList);
  451.         
  452.     if (myErr == noErr)
  453.         myErr = AESend(&myAppleEvent, &myReply, kAEWaitReply + kAEAlwaysInteract,
  454.                                     kAENormalPriority, kAEDefaultTimeout, gAEIdleUPP, NULL);
  455.       
  456.     AEDisposeDesc(&myAppleEvent);
  457.     AEDisposeDesc(¶mList);
  458.         // Dispose of textDesc before we get one in reply
  459.     AEDisposeDesc(&textDesc);
  460.     
  461.     if (myErr == noErr)
  462.         myErr = GetTextDescFromReply(&myReply, &textDesc);
  463.  
  464.     if (myErr == noErr)
  465.     {
  466.            myErr = SetSelection(&textDesc);
  467.            AEDisposeDesc(&textDesc);
  468.        }
  469.         
  470.     AEDisposeDesc(&myReply);
  471.         
  472.     return(myErr);
  473. }
  474.  
  475. //    Calls script subroutine that changes a given file's creator
  476. //    to the creator specified.
  477. //    Uses direct and predefined label parameters.
  478. //        on changecreator of aSpec into aCreator
  479.  
  480. pascal OSErr ExecuteScript4(DPtr theDoc)
  481. {
  482.     AppleEvent         myAppleEvent = {typeNull,NULL},
  483.                     ignoreReply = {typeNull,NULL};
  484.     AEDesc            selfAddress;
  485.     AEDescList       paramList = {typeNull,NULL};
  486.     OSErr           myErr;
  487.     Str255          pStr = "\pchangecreator";
  488.     
  489.     if (gScript4ID == kOSANullScript)
  490.         return(noErr);
  491.  
  492.     myErr = MakeSelfAddress(&selfAddress);
  493.     if (myErr != noErr) return myErr;
  494.         
  495.         // Create a subroutine AppleEvent
  496.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  497.                                                 kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  498.    
  499.            // Add the name of the subroutine
  500.     if (myErr == noErr)
  501.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  502.                                                    (Ptr)&pStr[1], pStr[0]);
  503.            
  504.            // Add the direct parameter                                                
  505.     if (myErr == noErr)
  506.            myErr = AEPutParamPtr(&myAppleEvent, keyDirectObject, typeFSS,    
  507.                                                 (Ptr)&(theDoc->theFSSpec), sizeof(FSSpec));
  508.  
  509.            // Add the subroutine parameter label                                            
  510.     if (myErr == noErr)
  511.     {
  512.         PLstrcpy(pStr, "\pToyS");
  513.            myErr = AEPutParamPtr(&myAppleEvent, keyASPrepositionInto, typeChar,    
  514.                                                         (Ptr)&pStr[1], pStr[0]);
  515.     }
  516.  
  517.         
  518.     if (myErr == noErr)
  519.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript4ID,
  520.                                                 kOSAModeAlwaysInteract, &ignoreReply);
  521.       
  522.       AEDisposeDesc(&selfAddress);
  523.     AEDisposeDesc(&myAppleEvent);
  524.     AEDisposeDesc(&ignoreReply);
  525.     
  526.     return(myErr);
  527. }
  528.  
  529.  
  530. //    Calls script subroutine to get this application's selection.
  531. //    Uses no parameters except for subroutine name.
  532. //        on getselection()
  533.  
  534. pascal    OSErr    GetSelection(AEDesc *textDesc)
  535. {
  536.     AppleEvent         myAppleEvent = {typeNull,NULL},
  537.                     myReply = {typeNull,NULL};
  538.     AEDesc            selfAddress;
  539.     OSErr           myErr;
  540.     Str255          pStr = "\pgetselection";
  541.     
  542.     if (gScript5ID == kOSANullScript)
  543.         return(noErr);
  544.  
  545.     myErr = MakeSelfAddress(&selfAddress);
  546.     if (myErr != noErr) return myErr;
  547.         
  548.                 // Create an AppleScript subroutine AppleEvent
  549.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  550.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  551.    
  552.                // Specify the routine by it's name
  553.     if (myErr == noErr)
  554.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  555.                                                                    (Ptr)&pStr[1], pStr[0]);
  556.  
  557.     if (myErr == noErr)
  558.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript5ID,
  559.                                                     kOSAModeAlwaysInteract, &myReply);
  560.  
  561.     myErr = GetTextDescFromReply(&myReply, textDesc);
  562.     
  563.       AEDisposeDesc(&selfAddress);
  564.     AEDisposeDesc(&myAppleEvent);
  565.     AEDisposeDesc(&myReply);
  566.         
  567.     return(myErr);
  568. }
  569.  
  570.  
  571. //    Calls script subroutine to set this application's selection.
  572. //    Uses a positional parameter.
  573. //        setselection(aString)
  574.  
  575. pascal    OSErr    SetSelection(AEDesc *textDesc)
  576. {
  577.     AppleEvent         myAppleEvent = {typeNull,NULL},
  578.                     myReply = {typeNull,NULL};
  579.     AEDesc            selfAddress;
  580.     AEDescList       paramList = {typeNull,NULL};
  581.     OSErr           myErr;
  582.     Str255          pStr = "\psetselection";
  583.     
  584.     if (gScript5ID == kOSANullScript)
  585.         return(noErr);
  586.  
  587.     myErr = MakeSelfAddress(&selfAddress);
  588.     if (myErr != noErr) return myErr;
  589.     
  590.             // Create an AppleScript subroutine AppleEvent
  591.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  592.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  593.    
  594.                // Specify the routine by it's name
  595.     if (myErr == noErr)
  596.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  597.                                                                    (Ptr)&pStr[1], pStr[0]);
  598.  
  599.             // Create positional parameter list
  600.     if (myErr == noErr)
  601.         myErr = AECreateList(NULL, 0 ,false, ¶mList);
  602.         
  603.             // Add textDesc as positional parameter
  604.     if (myErr == noErr)
  605.         myErr = AEPutDesc(¶mList, 0, textDesc);
  606.  
  607.             // Add positional parameter list
  608.     if (myErr == noErr)
  609.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, ¶mList);
  610.  
  611.     if (myErr == noErr)
  612.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript5ID,
  613.                                                     kOSAModeAlwaysInteract, &myReply);
  614.     
  615.       AEDisposeDesc(&selfAddress);
  616.     AEDisposeDesc(&myAppleEvent);
  617.     AEDisposeDesc(¶mList);
  618.     AEDisposeDesc(&myReply);
  619.         
  620.     return(myErr);
  621. }
  622.  
  623. // Takes a reply and tries to get a text descriptor from it.
  624.  
  625. pascal    OSErr    GetTextDescFromReply(AEDesc *aReply, AEDesc *textDesc)
  626. {
  627.     OSErr        myErr;
  628.  
  629.     textDesc->descriptorType = typeNull;
  630.     textDesc->dataHandle = NULL;
  631.  
  632.     myErr = AEGetParamDesc(aReply, keyAEResult, typeChar, textDesc);
  633.     
  634.     return(myErr);
  635. }
  636.  
  637.  
  638. pascal void EnableAEScriptItems(Boolean fEnable)
  639. {
  640.     if (fEnable && kOSANullScript != gScript1ID)
  641.             EnableItem(myMenus[scriptM], cScript1);
  642.     else
  643.             DisableItem(myMenus[scriptM], cScript1);
  644.     if (fEnable && kOSANullScript != gScript2ID)
  645.             EnableItem(myMenus[scriptM], cScript2);
  646.     else
  647.             DisableItem(myMenus[scriptM], cScript2);
  648.     if (fEnable && gScriptAppAddress.dataHandle)
  649.             EnableItem(myMenus[scriptM], cScript3);
  650.     else
  651.             DisableItem(myMenus[scriptM], cScript3);
  652.     if (fEnable && kOSANullScript != gScript4ID)
  653.             EnableItem(myMenus[scriptM], cScript4);
  654.     else
  655.             DisableItem(myMenus[scriptM], cScript4);
  656. }